package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.query.TravelQuery;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.util.UMEditorUploader;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/travel")
public class TravelController {

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;
    @Reference
    private ITravelCommentService travelCommentService;

    @Value("${file.dir}")
    private String dir;

    @RequestMapping("")
    public String list(@ModelAttribute("qo") TravelQuery qo, Model model) {
        //pageInfo
        model.addAttribute("pageInfo", travelService.query(qo));
        return "travel/list";
    }


    @RequireLogin
    @RequestMapping("/detail")
    public String detail(Long id, Model model) {
        Travel travel = travelService.get(id);
        travel.setTravelContent(travelService.getTravelContent(travel.getId()));
        //pageInfo
        model.addAttribute("detail", travel);

        Long destId = travel.getDest().getId();
        //吐司
        List<Destination> toasts = destinationService.listToasts(destId);
        model.addAttribute("toasts", toasts);
        //目的地
        //model.addAttribute("dest", toasts.remove(toasts.size() - 1));

        //sds :查询该目的地下点击量前3的攻略
        model.addAttribute("sds", strategyDetailService.listViewnumTop3(destId));
        //ts :查询该目的地下点击量前3的游记
        model.addAttribute("ts", travelService.listViewnumTop3(destId));

        //list :当前游记所有评论
        model.addAttribute("list", travelCommentService.listByTravelId(travel.getId()));

        return "travel/detail";
    }

    @RequireLogin//需要登录才能写游记
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            //只查旅游基本内容,跳到页面才发ajax
            model.addAttribute("tv", travelService.get(id));
        }
        //目的地
        model.addAttribute("dests", destinationService.listDests());
        return "travel/input";

    }

    @ResponseBody
    @RequestMapping("/getContent")
    public Object getContent(Long id) {
        return travelService.getTravelContent(id);

    }

    @RequireLogin
    @ResponseBody
    @RequestMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel entity, @LoginUser UserInfo user) {
        entity.setAuthor(user);//设置当前登录用户,因为远程调用对象的地址值不相同
        return travelService.saveOrUpdate(entity);
    }

    //游记封面的文件上传
    @ResponseBody
    @RequestMapping("/coverImageUpload")
    public Object coverImageUpload(MultipartFile pic) {
        //直接返回文件名
        return UploadUtil.upload(pic, dir);

    }

    //UME上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        java.lang.String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, dir);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }

    @RequestMapping("/commentAdd")
    public String commentAdd(TravelComment entity, Integer floor, @LoginUser UserInfo user, Model model) {
        //填充用户数据
        entity.setUserId(user.getId());
        entity.setUsername(user.getNickname());
        entity.setHeadUrl(user.getHeadImgUrl());
        entity.setLevel(user.getLevel());
        entity.setCity(user.getCity());//用户括号里的地点值
        //更新
        TravelComment comment = travelCommentService.save(entity);
        //c :游记评论对象
        model.addAttribute("c", comment);
        //floor楼层
        model.addAttribute("floor", floor + 1);
        return "travel/commentTpl";
    }
}
